<template>
  <div class="me-container">
    <div class="me-header">
      <div class="me-header-left">
        <div v-show="showHeaderLeft" class="me-header-left-avatar">
          <img :src="userAvatar" alt="用户头像">
        </div>
      </div>
      <div class="me-header-right">
        <div class="me-header-right-setting" @click="openSettings">
          <i class="icon-settings"></i>
          设置
        </div>
      </div>
    </div>

    <div class="me-content">
      <!-- 头像区域 -->
      <div class="avatar-section">
        <div class="avatar-info">
          <div class="avatar">
            <img :src="userAvatar" alt="用户头像">
          </div>
          <div class="user-text">
            <div class="username">{{ userInfo.name }}</div>
          </div>
        </div>
        <div class="description" @click="editDescription">
          <p v-if="!isEditingDesc">{{ userInfo.description || '这个人很懒，什么都没有留下' }}</p>
          <textarea
            v-else
            v-model="tempDescription"
            @blur="saveDescription"
            @keydown.enter="saveDescription"
            ref="descInput"
            rows="3"
            placeholder="写点什么来介绍自己..."
          ></textarea>
        </div>
      </div>

      <!-- 基本信息 -->
      <div class="user_info">
        <div class="left">
          <div class="user_info_item" v-for="(item, index) in showUserValueKeys" :key="index">
            <div class="user_info_item_value">
              {{ userInfo[item.key] }}
            </div>
            <div class="user_info_item_title">
              {{ item.label }}
            </div>
          </div>
        </div>
        <div class="right">
          <div class="edit-btn" @click="openSettings">
            <i class="icon-settings"></i>
            编辑资料
          </div>
        </div>
      </div>

      <!-- 帖子、运动 -->
      <div class="user_info_tabs">
        <div
          class="user_info_tabs_item"
          v-for="(item, index) in showUserTabs"
          :key="index"
          :class="{ active: currentIndex === index }"
          @click="handleTabClick(item, index)"
        >
          {{ item.label }}
        </div>
      </div>

      <div class="user_info_tabs_item_content">
        <transition :name="transitionName">
          <component :key="currentIndex" :is="currentTab.component" :id="userInfo.id" />
        </transition>
      </div>
    </div>

    <!-- 设置抽屉 -->
    <x-drawer 
      v-model="showSettings" 
      title="设置"
      position="right"
    >
      <div class="x-drawer-body">
        <div class="x-list">
          <div class="x-list__item" v-for="(item, index) in settingsList" :key="index">
            {{ item.label }}
          </div>
        </div>

        <div class="x-drawer-footer">
          <x-button type="danger" class="x-button-logout" @click="handleLogout">退出登录</x-button>
        </div>
      </div>
    </x-drawer>
  </div>
</template>

<script setup lang="ts">
import { ref, nextTick } from 'vue'
import XDrawer from '../../components/XDrawer/index.vue'
import UserRecords from './components/userRecords.vue'
import UserPosts from './components/userPosts.vue'
import { logout } from '../../api/user.js'
import { removeToken } from '../../utils/auth.js'
import { useRouter } from 'vue-router'

const router = useRouter()
const showSettings = ref(false)
const userAvatar = ref('/default-avatar.png') // 默认头像路径
const userInfo = ref({
  id: '12',
  name: '张三',
  description: '这个人很懒，什么都没有留下',
  email: 'zhangsan@example.com',
  phone: '12345678901',
  address: '北京市海淀区',
  fans: 100,
  follow: 100,
  post: 100,
  like: 100,
  collect: 100,
  comment: 100
})

//渲染
const showHeaderLeft = ref(false)
const showUserValueKeys = ref([
  { key: 'fans', label: '粉丝' },
  { key: 'follow', label: '关注' },
  { key: 'like', label: '点赞' }
]);
type UserTab = {
  key: string
  label: string
  component: any
}
const showUserTabs = ref<UserTab[]>([
  { key: 'posts', label: '帖子', component: UserPosts },
  { key: 'records', label: '我的运动', component: UserRecords },
  { key: 'likes', label: '我点赞的', component: UserPosts },
]);
const transitionName = ref('slide-left')
const currentIndex = ref(0)
const currentTab = ref<UserTab>(showUserTabs.value[0])

const settingsList = ref([
  { label: '个人信息' },
  { label: '通知设置' },
  { label: '隐私设置' },
  { label: '关于我们' }
])

const isEditingDesc = ref(false)
const tempDescription = ref('')
const descInput = ref<HTMLTextAreaElement>()

const editDescription = () => {
  tempDescription.value = userInfo.value.description
  isEditingDesc.value = true
  nextTick(() => {
    descInput.value?.focus()
  })
}

const saveDescription = () => {
  userInfo.value.description = tempDescription.value.trim() || '这个人很懒，什么都没有留下'
  isEditingDesc.value = false
}

const openSettings = () => {
  showSettings.value = true
}

const handleTabClick = (item: UserTab, index: number) => {
  transitionName.value = index > currentIndex.value ? 'slide-left' : 'slide-right'
  currentIndex.value = index
  currentTab.value = item
}

const handleLogout = async () => {
  try {
    await logout()
    removeToken()
    router.push('/Login')
  } catch (error) {
    console.error('登出失败:', error)
  }
}
</script>

<style scoped lang="scss">
.me-container {
  padding: 20px;
  padding-bottom: 50px;

  .me-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 5px;
  }

  .me-content {
    max-height: calc(100vh - 100px);
    overflow-y: auto;
    // 不显示滚动条
    &::-webkit-scrollbar {
      display: none;
    }
  }
}

.avatar-section {
  margin-bottom: 30px;
  padding: 0 10px;

  .avatar-info {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
  }

  .avatar {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    overflow: hidden;
    border: 2px solid #f0f0f0;
    flex-shrink: 0;
    
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }

  .user-text {
    margin-left: 15px;

    .username {
      font-size: 20px;
      font-weight: 600;
      color: #333;
      margin-bottom: 8px;
    }
  }

  .description {
    position: relative;
    
    p {
      font-size: 14px;
      color: #666;
      line-height: 1.6;
      margin: 0;
      padding: 8px 0;
      cursor: pointer;
      
      &:hover {
        background-color: #f9f9f9;
      }
    }

    textarea {
      width: 100%;
      border: 1px solid #ddd;
      border-radius: 8px;
      padding: 8px 12px;
      font-size: 14px;
      line-height: 1.6;
      resize: none;
      outline: none;
      transition: border-color 0.3s ease;

      &:focus {
        border-color: #007bff;
      }
    }
  }
}

// .button-section {
//   position: absolute;
//   width: calc(100% - 80px);
//   left: 40px;
//   bottom: 120px;
//   display: flex;
//   flex-direction: column;
//   gap: 15px;
// }

:deep(.user_info) {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px 10px;
  border-radius: 10px;
  border: 1px solid #ccc;

  .left {
    display: flex;
    gap: 10px;

    .user_info_item {
    }

    .user_info_item_value {
      font-size: 14px;
      font-weight: bold;
    }

    .user_info_item_title {
      font-size: 10px;
    }
  }

  .right {
  }
}

.user_info_tabs {
  display: flex;
  margin: 20px 0;
  border-bottom: 1px solid #eee;
  
  &_item {
    position: relative;
    padding: 12px 24px;
    font-size: 15px;
    color: #666;
    cursor: pointer;
    transition: all 0.3s ease;

    &::after {
      content: '';
      position: absolute;
      bottom: -1px;
      left: 0;
      width: 100%;
      height: 2px;
      background-color: transparent;
      transition: all 0.3s ease;
    }

    &:hover {
      color: #333;
    }

    &.active {
      color: var(--color-primary);
      font-weight: 500;

      &::after {
        background-color: var(--color-primary);
      }
    }
  }
}

.user_info_tabs_item_content {
  min-height: 200px;
  padding: 16px 0;
}

:deep(.edit-btn) {
  display: inline-flex;
  align-items: center;
  padding: 4px 12px;
  font-size: 13px;
  color: #666;
  background-color: #f5f5f5;
  border-radius: 15px;
  cursor: pointer;
  transition: all 0.3s ease;

  &:hover {
    background-color: #e8e8e8;
  }

  .icon-settings {
    margin-right: 4px;
    font-size: 14px;
  }
}

.slide-left-enter-active,
.slide-left-leave-active,
.slide-right-enter-active,
.slide-right-leave-active {
  transition: transform 0.5s ease;
}

.slide-left-enter-from {
  transform: translateX(100%);
}

.slide-left-leave-to {
  transform: translateX(-100%);
}

.slide-right-enter-from {
  transform: translateX(-100%);
}

.slide-right-leave-to {
  transform: translateX(100%);
}

:deep(.x-drawer-body) {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  height: 100%;
}

:deep(.x-drawer-footer) {
  margin-bottom: 10px;

  .x-button-logout {
    width: 100%;
  }
}
</style>
